package org.cc.cloud;


import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

	@RequestMapping(value="hello/{name}",method=RequestMethod.GET)
	public String  hello(@PathVariable String  name)
	{
		return "hello "+name;
	}
	
	@RequestMapping(value="hellown",method=RequestMethod.GET)
	public String  helloWithNoArg()
	{
		return "hello word";
	}
	@RequestMapping(value="helloerror",method=RequestMethod.GET)
	public String  helloerror() throws InterruptedException
	{
		Thread.sleep(10000);
		return "hello word";
	}
	@RequestMapping(value="call/{id}",method=RequestMethod.GET)
	public Provider Provider(@PathVariable Integer id,HttpServletRequest requset)
	{
		Provider  v=new Provider();
		v.setId(id);
		v.setName("测试服务");
		v.setUrl(requset.getRequestURL().toString());
		v.setUri(requset.getRequestURI().toString());
		return v;
//		v.setUri(requst.getu);
	}
	//注意@RequestBody和@ResponseBody
	@RequestMapping(value="call/create",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
	public void CreateProvider(@RequestBody Provider provider)
	{
		System.out.println(provider.getName());
	}
	//注意@RequestBody和@ResponseBody
	@RequestMapping(value="call/create1",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
	public Provider CreateProvider1(@RequestBody Provider provider)
	{
		provider.setId(123);
		return provider;
	}
	
	/**
	 * 参数与返回值均为XML
	 */
	@RequestMapping(value = "/call/createXML", method = RequestMethod.POST, 
			consumes = MediaType.APPLICATION_XML_VALUE, 
			produces = MediaType.APPLICATION_XML_VALUE)
	public String createXMLPerson(@RequestBody Provider person) {
		System.out.println(person.getName() + "-" + person.getId());
		return "<result><message>success</message></result>";
	}
	public static boolean  canVisitDB=true;
	@RequestMapping(value="db/{can}",method=RequestMethod.GET)
	public void setDB(@PathVariable boolean  can)
	{
		canVisitDB=can;
	}
}
